﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSNPro.Utils
{
    public class IniFile
    {
        private readonly string filePath;

        public IniFile(string path)
        {
            filePath = path;
        }

        // 读取 INI 文件中的值
        public string Read(string section, string key)
        {
            StringBuilder result = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", result, result.Capacity, filePath);
            return result.ToString();
        }

        public int ReadInt(string section, string key)
        {
            string val=Read(section, key);
            return Convert.ToInt32(val);
        }

        // 写入 INI 文件中的值
        public void Write(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, filePath);
        }

        // 调用 Windows API 读取 INI 文件
        [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        // 调用 Windows API 写入 INI 文件
        [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern int WritePrivateProfileString(string section, string key, string value, string filePath);
    }

}
